<template>
    <div>
        <el-table :data="list" border style="width: 100%">
            <el-table-column prop="supplierId" label="供应商" width="100"></el-table-column>
            <el-table-column prop="goodsId" label="货品" width="180"></el-table-column>
            <el-table-column prop="number" label="数量" width="180"></el-table-column>
            <el-table-column prop="price" label="价格"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 弹出框 -->
        <div>
            <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose"
            >
                <span>修改信息</span>
                <el-form
                    :model="ruleForm"
                    :rules="rules"
                    ref="ruleForm"
                    label-width="100px"
                    class="demo-ruleForm"
                >
                    <el-form-item label="数量" prop="number">
                        <el-input v-model="ruleForm.number" @change="changT()"></el-input>
                    </el-form-item>
                    <el-form-item label="价格" prop="price">
                        <el-input v-model="ruleForm.price"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { getOrder ,getListAllOne} from '../../../api/supplier';
import { mapState, MapActions, mapActions } from 'vuex';
import Axios from 'axios';
export default {
    data() {
        return {
            optionsList: [],
            list: [],
            dialogVisible: false,
            ruleForm: {
                goodsId:'',
                number: '',
                price: '',
                unitprice: '',
                _id: ''
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        async changT() {
            const _id = this.ruleForm.goodsId;
            const data = await getListAllOne(_id);
            this.ruleForm.unitprice = data[0].price;
            const number = this.ruleForm.number;
            const unitprice = this.ruleForm.unitprice;
            
            this.ruleForm.price = number * unitprice;
            console.log(this.ruleForm.price);
        },
        handleEdit(index, row) {
            this.dialogVisible = true;
            this.ruleForm.goodsId = row.goodsId;
            this.ruleForm.number = row.number;
            this.ruleForm.price = row.price;
            this.ruleForm._id = row._id;
        },
        submitForm(formName, row) {
            this.dialogVisible = false;
            const _id = this.ruleForm._id;
            Axios({
                method: 'patch',
                url: `/api/supplier/order/${_id}`,
                data: {
                    number: this.ruleForm.number,
                    price: this.ruleForm.price
                }
            }).then(res => {
                this.getList();
            });
        },
        handleDelete(index, row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    Axios({
                        method: 'post',
                        url: '/api/supplier/delOrder',
                        data: { _id: row._id }
                    }).then(res => {
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        async getList() {
            const data = await getOrder();
            this.list = data.rows;
        }
    }
};
</script>

<style></style>
